/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
*/


/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   ********************** */
function preLoad() {
	if (!this.support.loading) {
		alert("You need the Flash Player 9.028 or above to use SWFUpload.");
		return false;
	}
}
function loadFailed() {
	//alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative");
}
var global_queued=0;

function fileQueued(file) {
	
	try {
		//this.customSettings.tdFilesQueued.innerHTML = '<table border="1">'+queued_str+"</table>";//this.getStats().files_queued;
	} catch (ex) {
		this.debug(ex);
	}

}

function fileDialogComplete() {
	if(this.getStats().files_queued!=0){
	var queued_str = '';
	var queue_i=0;
			for(;queue_i<this.getStats().files_queued;queue_i++){
				queued_str+='<tr><td valign="top"><div class="photoids" style="display:none"></div><div style="width:64px;height:64px;background:#ccc" class="thumb_">0%</div></td><td valign="top"><dl class="photo_row"><dd><div class="current_baby">宝宝</div><div class="baby_focus">我关注的宝宝:</div><div class="baby_mine">我的宝宝</div></dd><dd><div class="inputs">输入框</div></dd><dd><labe class="label_set">分类标签:[用于将相片自定义分类，动态添加稍候加上去]<div><ul><li><label><input type="checkbox" name="clabel[27][]" value="1">分类1</label></li><li><label><input type="checkbox" name="clabel[27][]" value="2">分类2</label></li></ul></div></labe></dd><dd><div class="shootingdates">拍摄日期</div></dd></dl></div><div class="alert uploaded" style="display:none">已上传!</div></td></tr>';
			}
			$("#queue_body").append(queued_str);
		
	}
	this.startUpload();
}

function uploadStart(file) {
	try {
		this.customSettings.progressCount = 0;
		updateDisplay.call(this, file);
	}
	catch (ex) {
		this.debug(ex);
	}
	
}
var thumb_i = 0;
var progress_i = 0;
var progress_pre = 0;
function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		
		
		this.customSettings.progressCount++;
		
		
		updateDisplay.call(this, file);

		
	} catch (ex) {
		this.debug(ex);
	}
	
}

function uploadSuccess(file, serverData) {
	try {
		
		updateDisplay.call(this, file);
		var json = eval(serverData);
		var baby_chkbox = '';
		var bi = 0;
		var baby_mine='',baby_focus='';
		var obj_thumb = $(".thumb_");
		var obj_row = $(".photo_row");
		var obj_uploaded = $(".uploaded");
		
		if(json[0]['flag']=='success'){
		
		var obj_cbaby = $(".current_baby");
		var obj_input = $(".inputs");
		var obj_date = $(".shootingdates");
		var obj_bf = $(".baby_focus");
		var obj_bm = $(".baby_mine");
		var obj_ptid = $(".photoids");	
				//if(baby_json['baby_id']==current_babyid){
				baby_chkbox+='<label>当前宝宝:<input type="checkbox" name="baby_id['+json[0]['id']+'][]" value="'+baby_json['baby_id']+'" checked="checked" />'+baby_json['baby_realname']+'</label>';
		
		$.each(baby_mine_json,function(bm_index,bm_data){
			baby_mine+='<label><input type="checkbox" name="baby_id['+json[0]['id']+'][]" value="'+bm_data['baby_id']+'">'+bm_data['baby_realname']+'<small>'+bm_data['baby_nickname']+'</small></label>';
		});
		$.each(baby_focus_json,function(bf_index,bf_data){
			baby_focus+='<label><input type="checkbox" name="baby_id['+json[0]['id']+'][]" value="'+bf_data['baby_id']+'">'+bf_data['baby_realname']+'<small>'+bf_data['baby_nickname']+'</small></label>';
		});
				
			
		obj_cbaby[thumb_i].innerHTML=baby_chkbox;
		obj_bm[thumb_i].innerHTML = '我的宝宝:'+baby_mine;
		obj_bf[thumb_i].innerHTML = '我关注的宝宝:'+baby_focus;
		
		obj_ptid[thumb_i].innerHTML = '<input type="hidden" name="photo_id[]" value="'+json[0]['id']+'">';
		obj_input[thumb_i].innerHTML='<label class="label_set">标题:</label><input type="text" value="未命名" name="photo_title['+json[0]['id']+'][]">';
		obj_date[thumb_i].innerHTML='<label class="label_set">拍摄日期:</label>'+json[0]['ShootingDate'];
		obj_thumb[thumb_i].innerHTML='<img src="'+json[0]['picurl']+'" width="64" height="64">';
		
		}else if(json[0]['flag']=='exist'){
		//已经上传
			//photo_row
			
			obj_thumb[thumb_i].innerHTML='<img src="'+json[0]['picurl']+'" width="64" height="64">';
			obj_row[thumb_i].style.display="none";
			obj_uploaded[thumb_i].style.display="block";
		}
thumb_i++;
		
		

		
		
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
//	this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
//	this.customSettings.tdFilesUploaded.innerHTML = this.getStats().successful_uploads;
//	this.customSettings.tdErrors.innerHTML = this.getStats().upload_errors;
$("#btn_submit").show();
}

function updateDisplay(file) {
	var obj_thumb = $(".thumb_");
	obj_thumb[thumb_i].innerHTML=SWFUpload.speed.formatPercent(file.percentUploaded);


}